/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import org.aergia.vinny.core.enums.DayOfWeek;
import org.aergia.vinny.database.pojo.OperatingHours;
import org.aergia.vinny.database.pojo.VinnyUser;
import org.aergia.vinny.gui.controller.VinnyController;
import org.aergia.vinny.gui.types.ColumnDefinition;
import org.aergia.vinny.util.DateUtil;

public class OperatingHoursPanel extends DatabasePojoPanel {

	private static final long serialVersionUID = 1L;

	/**
	 * @param controller
	 * @param user
	 */
	public OperatingHoursPanel(final VinnyController controller, final VinnyUser user) {
		super(controller, user, new OperatingHoursTableModel(controller), false, true);
		final HourCellRender render = new HourCellRender();
		this.setCellRenderer("Start Hour", render);
		this.setCellRenderer("End Hour", render);
	}

	@Override
	protected void addButtonActionPerformed(final ActionEvent event) {
		// ignore
	}

	@Override
	protected void deleteButtonActionPerformed(final ActionEvent event) {
		// ignore
	}

	@Override
	protected void editButtonActionPerformed(final ActionEvent event) {
		final int selectedRow = this.getSelectedRow();
		if (selectedRow == -1) {
			return;
		}
		final OperatingHoursTableModel model = (OperatingHoursTableModel) this.tableModel;
		final OperatingHours hours = model.getRecord(selectedRow);
		final OperatingHoursEditor editor = new OperatingHoursEditor(this.controller, this.user, hours);
		final EditorDialog dialog = new EditorDialog(editor);

		if (dialog.isSaved()) {
			model.setRecord(selectedRow, hours);
			this.tableModel.fireTableRowsUpdated(selectedRow, selectedRow);
			this.refresh();
		}
	}

	private class HourCellRender extends DefaultTableCellRenderer {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public HourCellRender() {
			// ignore
		}

		@Override
		public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row,
				final int column) {

			return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		}

		@Override
		public void setValue(final Object value) {
			if (value instanceof Integer) {
				final int intValue = ((Integer) value).intValue();
				if (intValue == 0) {
					this.setText("");
				} else {
					this.setText(DateUtil.formatHour(((Integer) value).longValue()));
				}
			} else {
				this.setText(value.toString());
			}
		}
	}

	private static class OperatingHoursTableModel extends DatabasePojoTableModel<OperatingHours> {

		private static List<ColumnDefinition> definitions;

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		static {
			OperatingHoursTableModel.definitions = new ArrayList<ColumnDefinition>();
			OperatingHoursTableModel.definitions.add(new ColumnDefinition("Day", String.class, 120));
			OperatingHoursTableModel.definitions.add(new ColumnDefinition("Start Hour", Integer.class, 120));
			OperatingHoursTableModel.definitions.add(new ColumnDefinition("End Hour", Integer.class, 120));
		}

		public OperatingHoursTableModel(final VinnyController controller) {
			super(controller, OperatingHoursTableModel.definitions);
		}

		@Override
		public Object getValueAt(final int rowIndex, final int columnIndex) {
			final OperatingHours hours = this.records.get(rowIndex);
			switch (columnIndex) {
				case 0:
					return hours.getDayOfWeek().toString();
				case 1:
					return Integer.valueOf(hours.getStartHour());
				case 2:
					return Integer.valueOf(hours.getEndHour());
				default:
					return null;
			}
		}

		@Override
		protected void populateTable() {
			this.records.add(this.controller.getHours(DayOfWeek.SUNDAY));
			this.records.add(this.controller.getHours(DayOfWeek.MONDAY));
			this.records.add(this.controller.getHours(DayOfWeek.TUESDAY));
			this.records.add(this.controller.getHours(DayOfWeek.WEDNESDAY));
			this.records.add(this.controller.getHours(DayOfWeek.THURSDAY));
			this.records.add(this.controller.getHours(DayOfWeek.FRIDAY));
			this.records.add(this.controller.getHours(DayOfWeek.SATURDAY));
		}
	}
}
